<template>
  <div>
    <mytools>
      <template #right>
        <el-button
          type="primary"
          size="mini"
          @click="$router.push('/employees/import')"
          >导入</el-button
        >
        <el-button type="primary" size="mini" @click="openAdd"
          >+ 新增员工</el-button
        >
      </template>
    </mytools>

    <el-card class="mycard">
      <el-table :data="list" border show-header>
        <el-table-column type="index" label="序号" width="120">
        </el-table-column>
        <el-table-column prop="username" label="姓名" width="220">
        </el-table-column>
        <el-table-column label="头像" width="220">
          <template slot-scope="{ row }">
            <img
              class="img"
              @click="openEwm(row.staffPhoto)"
              v-errorimg="defaultImg"
              :src="row.staffPhoto"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" width="220">
        </el-table-column>
        <el-table-column prop="workNumber" label="工号" width="220">
        </el-table-column>
        <el-table-column prop="formOfEmployment" label="聘用形式" width="300">
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myEmployees }}
          </template>
        </el-table-column>
        <el-table-column prop="departmentName" label="部门" width="200">
        </el-table-column>
        <el-table-column label="入职时间" width="200">
          <template slot-scope="{ row }">
            {{ row.timeOfEntry | myFormat }}
          </template>
        </el-table-column>
        <el-table-column label="是否在职" width="200">
          <template>
            在职
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200">
          <template>
            可用
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="{ row }">
            <el-button :disabled="'employees-check' | hasRight" type="text" size="small" @click="check(row)"
              >查看</el-button
            >
            <el-button :disabled="'employees-turn' | hasRight" type="text" size="small">转正</el-button>
            <el-button type="text" size="small">调岗</el-button>
            <el-button type="text" size="small">离职</el-button>
            <el-button type="text" size="small" @click="addRole(row)"
              >角色</el-button
            >
            <el-button :disabled="'employees-delete' | hasRight" type="text" size="small">删除</el-button> 
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增面板 -->
    <mypanel :showDialog.sync="show"></mypanel>
    <!-- 显示二维码面板 -->
    <el-dialog title="显示二维码" :visible.sync="showEwm">
      <div class="box">
        <canvas ref="myCanvas" />
      </div>
    </el-dialog>

    <!-- 角色面板 -->
    <el-dialog title="分配角色" :visible.sync="showRole">
      <el-form>
        <el-form-item>
          <el-checkbox-group v-model="checkList">
            <el-checkbox
              v-for="(item, index) in roleList"
              :key="index"
              :label="item.id"
            >
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item class="myitem">
          <el-button @click="showRole = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import QrCode from 'qrcode'
import { getEmployeesListApi, getEmployeesApi, setRoleApi } from '@/api/employees.js'
import { getRoleApi } from '@/api/setting'
export default {
  name: 'employees',
  components: {
    mypanel: () => import('./components/mypanel.vue')
  },
  data () {
    return {
      list: [{ name: 'ww' }],
      currentPage: 1,
      pageSizes: [8, 16, 20, 24],
      pageSize: 8,
      total: 0,
      show: false,
      defaultImg: require('@/assets/common/bigUserHeader.png'),
      showEwm: false,
      showRole: false,
      checkList: [],
      roleList: [],
      currentObj: []
    }
  },
  methods: {
    // 页容量改变触发
    sizeChange (size) {
      this.pageSize = size
      this.getEmployeesList()
    },
    // 当前页改变触发
    currentChange (page) {
      this.currentPage = page
      this.getEmployeesList()
    },
    // 获取员工列表
    async getEmployeesList () {
      try {
        const res = await getEmployeesListApi({
          page: this.currentPage,
          size: this.pageSize
        })
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 打开新增面板
    openAdd () {
      this.show = true
    },
    // 查看详细信息
    check (row) {
      // console.log(row);
      this.$router.push(`/employees/details/${row.id}`)
    },
    // 点击图片显示二维码
    openEwm (img) {
      // console.log(img);
      this.showEwm = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.myCanvas, img, { width: 400, height: 400 })
      })
    },
    // 获取分配角色
    async addRole (row) {
      this.currentObj= row
      this.getRoleList()
      const res = await getEmployeesApi(row.id)
      // console.log(res);
      this.checkList = res.data.roleIds
      this.showRole = true
      // console.log(row)
    },
    // 获取角色列表
    async getRoleList () {
      const res = await getRoleApi()
      // console.log(res);
      this.roleList = res.data.rows
    },
    // 给员工分配角色
    async submit() {
       
       await setRoleApi({
         id: this.currentObj.id,
         roleIds: this.checkList
       })

       this.$message.success('分配角色成功')
       this.showRole = false
       this.getEmployeesList()
    }
  },
  created () {
    this.getEmployeesList()
  }
}
</script>

<style lang="scss">
.mycard {
  margin: 20px;

  .pagination {
    margin-top: 20px;
  }

  .el-table__header {
    th {
      text-align: center;
    }
  }
  .el-table td {
    text-align: center;
  }

  .img {
    width: 60px;
    height: 60px;
  }
}

.box {
  text-align: center;
}
.myitem {
  text-align: center;
}
</style>
